﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class LoginFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }   
  /// <summary>
  /// 根据用户名，读取用户的角色ID
  /// </summary>
  /// <param name="userName">用户名</param>
  /// <returns>返回用户的角色ID</returns>
    protected int  FindUserRole(string userName)
    {
        string strFindRole = "select ROLEID from USERS where USERNAME='"+userName+"'";
        DBOperation db = new DBOperation();
        int temp =Convert.ToInt32( db.ExcuteScalar(strFindRole));
        return temp;
    }
    //检测是否有本年度的表，没有就创建
    protected void CreateTable()
    {
        DBOperation.CreateTable();
    }
    protected Boolean AddLoginLog(string userName)
    {
        Log userLoginLog = new Log();
        userLoginLog.Title = userName + "登录系统";
        userLoginLog.Content = userName + "于" + DateTime.Now.ToString() + "成功登陆系统。登录IP:" + Request.UserHostAddress.ToString();
        userLoginLog.UserName = userName;
        userLoginLog.AddDate = DateTime.Now;
        userLoginLog.Status = "1";
        userLoginLog.AddLog();
        return true;
    }
    protected void tijiao_Click(object sender, ImageClickEventArgs e)
    {
        CreateTable();
        //关闭或者打开每月自动备份数据库功能
        //AutoBackupData();
        if (Validated.validatedUser(txtName.Text.Trim(), txtPwd.Text.Trim()))
        {

            Session["userName"] = txtName.Text.Trim();
            Session["roleID"] = FindUserRole(txtName.Text.Trim());
            AddLoginLog(Session["userName"].ToString());
            //通过登录验证后，跳转到默认主页，同时建立一个session记录当前用户，其他页面可以判断这个session是否为空来判断用户是否登录
            Response.Redirect("~/Default.aspx");
            //Response.Redirect(GetUrlPath.GetUrl + "/Default.aspx");


        }
        else
        {
            msgFail.Visible = true;
        }
    }
    //每月28号备份一次数据库
    protected void AutoBackupData()
    {
        int day = DateTime.Now.Day;
      
      if(day==28)
      {
          DBOperation db=new DBOperation();
          string strCheck = "select AUTOID from DATABACKUP where DAY(BAKTIME)="+day;
          //判断今天是否已经有了备份，如果没有就进行备份
          if(db.ExcuteScalar(strCheck)==null)
          {
              DataBackup bak = new DataBackup();
              DateTime backTime = bak.BakTime;
              string dataBaseName = "XSSFSYSTEM" + string.Format("{0:yyyyMMddHHmm}", backTime) + ".bak";
              bak.BakName = dataBaseName;
              string strPath = Server.MapPath("~\\files\\backup\\") + dataBaseName;
              if (bak.Backup("XSSFSYSTEM", strPath))
              {
                  bak.GetDatabaseSize(Server.MapPath("~\\files\\backup\\") + dataBaseName);
                  bak.InsertNewRecord();                  
              }
             
          }
      }
    }
}
